import json
import re

import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
}

url = 'https://ai.youdao.com/DOCSIRMA/html/trans/api/wbfy/index.html#section-9'


# 获取网易翻译支持语言类型接口
def get_json_data():
    data = [['英文名', '中文名', '代码']]

    resp = requests.get(url, headers=headers)
    resp.encoding = 'utf-8'
    page_text = resp.text.replace('\n', '')
    text = re.search(r'<p>下表为各语言对应代码：</p><table><thead>(.*?)</tbody></table>', page_text)
    soup = BeautifulSoup(text.group(), 'html.parser')
    tr_lst = soup.find_all('tr')[1:-1]

    for row in tr_lst:
        # 英文名
        en_td = row.find_all('td')[0]
        en = re.findall(r'>(.*)<', str(en_td))[0]
        # 中文名
        ch_td = row.find_all('td')[1]
        ch = re.findall(r'>(.*)<', str(ch_td))[0]
        # 代码
        code_td = row.find_all('td')[2]
        code = re.findall(r'>(.*)<', str(code_td))[0]

        data.append([en, ch, code])

    resp.close()
    return json.dumps(data, ensure_ascii=False)